import java.io.*;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args){

        int countOfThreads = 1;
        String inputFile = null, outputDirectory = null;
        try{
            for(int i = 0; i < args.length;){
                if(args[i].compareTo("-i") == 0)
                    inputFile = args[i+1];
                else if(args[i].compareTo("-o") == 0)
                    outputDirectory = args[i+1];
                else if(args[i].compareTo("-n") == 0)
                    countOfThreads = Integer.parseInt(args[i+1]);
                i += 2;
            }
        }catch (Exception ex){
            System.err.println("Incorrect param values" + "\n");
            System.exit(1);
        }
        if(outputDirectory.charAt(outputDirectory.length() - 1) != '/')
            outputDirectory += '/';
        if(! new File(outputDirectory).isDirectory()){
            System.err.println("Output directory is not exist");
            System.exit(4);
        }
        ThreadObserver observer = new ThreadObserver(countOfThreads, outputDirectory, readFile(inputFile));
        new Thread(observer).start();
    }

    private static ArrayList<String> readFile(String inputFile){
        ArrayList<String> links = new ArrayList<String>();
        String s;
        BufferedReader reader = null;
        try{
            reader = new BufferedReader(new FileReader(inputFile));
            while ((s = reader.readLine()) != null){
                links.add(s);
            }
        }catch (FileNotFoundException ex){
            System.err.println("Input file not found" + "\n");
            System.exit(2);
        }catch (IOException ex2){
            System.err.println("Error reading input file" + "\n");
            System.exit(3);
        }finally {
            if(reader != null)
                try{
                    reader.close();
                }catch (IOException ex){
                    System.err.println("Closing input file reader error" + "\n");
                }
        }
        return links;
    }

}
